package product

import (
	"github.com/xinliangnote/go-gin-api/internal/pkg/core"
	"github.com/xinliangnote/go-gin-api/internal/repository/mongodb/operation_log"
	"github.com/xinliangnote/go-gin-api/internal/repository/mongodb/product"
	userModel "github.com/xinliangnote/go-gin-api/internal/repository/mongodb/user"
	"time"
)

type CreateProductData struct {
	Id           string
	ProductName  string
	Fee          string
	Months       int
	Type         string
	DeleteStatus string
	CategoryId   string
	CategoryName string
}

func (s *service) Create(ctx core.Context, productData *CreateProductData) (id string, err error) {
	model := product.NewModel()
	model.ProductName = productData.ProductName
	model.Fee = productData.Fee
	model.Months = productData.Months
	model.Type = productData.Type
	model.DeleteStatus = "0"
	model.CategoryId = productData.CategoryId
	model.CategoryName = productData.CategoryName

	id, err = model.Create(s.db.GetDb())
	if err != nil {
		return "", err
	}

	userInfo := ctx.GetInfo("userInfo").(*userModel.User)
	typeMap := make(map[string]string)
	typeMap["1"] = "年"
	typeMap["2"] = "月"
	typeMap["3"] = "天"
	typeMap["4"] = "时"
	logModel := operation_log.NewModel()
	logModel.FTable = "product"
	logModel.Type = "add"
	logModel.FId = id
	logModel.IpAddr = ctx.GetClientIP()
	logModel.AdminName = userInfo.Username
	logModel.Content = "新增套餐:套餐类别:[" + productData.CategoryName + "],套餐名称:[" + productData.ProductName + "],套餐费用:[" + productData.Fee + "],套餐时间:[" + string(rune(productData.Months)) + typeMap[productData.Type] + "]"
	logModel.Timestamp = time.Now().Unix()
	logModel.Create(s.db.GetDb())

	return id, err
}

func (s *service) List(ctx core.Context, pageIndex uint, pageSize uint) (productList []product.Product, err error) {
	model := product.NewModel()
	list, err := model.List(s.db.GetDb(), pageIndex, pageSize)
	return list, err
}

func (s *service) Update(ctx core.Context, productData *CreateProductData) error {
	model := product.NewModel()
	model.ProductName = productData.ProductName
	model.Fee = productData.Fee
	model.Months = productData.Months
	model.Type = productData.Type
	model.Id = productData.Id
	model.CategoryId = productData.CategoryId
	model.CategoryName = productData.CategoryName
	err := model.Update(s.db.GetDb())

	if err == nil {
		userInfo := ctx.GetInfo("userInfo").(*userModel.User)
		typeMap := make(map[string]string)
		typeMap["1"] = "年"
		typeMap["2"] = "月"
		typeMap["3"] = "天"
		typeMap["4"] = "时"
		logModel := operation_log.NewModel()
		logModel.FTable = "product"
		logModel.Type = "edit"
		logModel.FId = productData.Id
		logModel.IpAddr = ctx.GetClientIP()
		logModel.AdminName = userInfo.Username
		logModel.Content = "更新套餐:套餐id[" + productData.Id + "],套餐类别:[" + productData.CategoryName + "],套餐名称:[" + productData.ProductName + "],套餐费用:[" + productData.Fee + "],套餐时间:[" + string(rune(productData.Months)) + typeMap[productData.Type] + "]"
		logModel.Timestamp = time.Now().Unix()
		logModel.Create(s.db.GetDb())

	}
	return err

}

func (s *service) Delete(ctx core.Context, productData *CreateProductData) error {
	model := product.NewModel()
	model.Id = productData.Id
	model.DeleteStatus = "1"
	err := model.Delete(s.db.GetDb())

	if err == nil {
		userInfo := ctx.GetInfo("userInfo").(*userModel.User)
		logModel := operation_log.NewModel()
		logModel.FTable = "product"
		logModel.Type = "del"
		logModel.FId = productData.Id
		logModel.IpAddr = ctx.GetClientIP()
		logModel.AdminName = userInfo.Username
		logModel.Content = "删除套餐:套餐id[" + productData.Id + "]"
		logModel.Timestamp = time.Now().Unix()
		logModel.Create(s.db.GetDb())

	}
	return err
}

func (s *service) Count(ctx core.Context) (int64, error) {
	model := product.NewModel()
	count, err := model.Count(s.db.GetDb())
	return count, err
}
